{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0ec2a26",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person:\n",
    "  name = 'Willianm'\n",
    "  age = 45 \n",
    "  def __init__(self, initName, initAge):\n",
    "    self.name = initName\n",
    "    self.age = initAge\n",
    "    \n",
    "  def greet(self):\n",
    "    print('Hi. my name is ' + self.name)\n",
    "\n",
    "p1 = Person('wanhong', 18)\n",
    "print(p1.age)\n",
    "print(p1.name)\n",
    "# p1.age = 40\n",
    "# p1.name = 'lilei'\n",
    "# p1.greet()\n",
    "# del p1.age\n",
    "# print(p1.age)\n",
    "# del p1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d146499c",
   "metadata": {},
   "source": [
    "## 继承和多态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8052a462",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Animal:\n",
    "  \n",
    "  def __init__(self, initName):\n",
    "        self.name = initName\n",
    "\n",
    "  def greet(self):\n",
    "    print('Hi, my name is' + self.name)\n",
    "\n",
    "    \n",
    "class Dog(Animal):\n",
    "    def greet(self):\n",
    "        print('wangwang Hi, my name is' + self.name)\n",
    "    def run(self):\n",
    "        print(self.name + 'is running')\n",
    "    \n",
    "d1 = Dog('二哈')\n",
    "d1.greet()\n",
    "d1.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2030e46e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Fib():\n",
    "  def __init__(self):\n",
    "    self.a ,self.b = 0 , 1\n",
    "  def __iter__(self):\n",
    "    return self\n",
    "  def __next__(self):\n",
    "    self.a, self.b = self.b, self.a + self.b\n",
    "    return self.a\n",
    "fib = Fib()\n",
    "for i in fib:\n",
    "  if i > 20:\n",
    "    break\n",
    "  print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea786191",
   "metadata": {},
   "source": [
    "## 访问限制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38621fa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "class Person:\n",
    "  __name = 'xianzhi'\n",
    "  def __init__(self, initName):\n",
    "      self.__name = initName\n",
    "  def greet(self):\n",
    "    print('Hi. my name is ' + self.__name)\n",
    "\n",
    "p1 = Person('wanhong')\n",
    "p1.greet()\n",
    "\n",
    "print(p1.__name) # error\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d744b65",
   "metadata": {},
   "source": [
    "## 静态函数和静态方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b3bece0",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Dog(object):\n",
    "    food = \"gutou\"\n",
    "    age = \"1\"\n",
    "    def __init__(self, name):\n",
    "        self.NAME = name\n",
    "    @classmethod\n",
    "    def eat(self,age): #只能是类中的变量\n",
    "        # print(self.NAME) # 报错\n",
    "        print(age)\n",
    "        print(self.food)\n",
    "\n",
    "    @classmethod\n",
    "    def eat1(self, age):  # 只能是类中的变量\n",
    "        # print(self.NAME) # 报错\n",
    "        age = \"2\"\n",
    "        self.food = \"tang\"\n",
    "    @staticmethod\n",
    "    def print_1():\n",
    "        print(Dog.food, Dog.age)\n",
    "\n",
    "d = Dog(\"labuladuo\")\n",
    "d.eat(Dog.age)    #通过对象调用\n",
    "Dog.eat(Dog.age)  #通过类调用\n",
    "print(\"-----1-----\")\n",
    "d.eat1(Dog.age)\n",
    "Dog.print_1()\n",
    "print(\"--------2-------\")\n",
    "Dog.eat1(Dog.age)\n",
    "Dog.print_1()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d17575a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
